/*
 * Class: ConfigView
 * 
 * Created on Sep 26, 2013
 * 
 * (c) Copyright by Minh Tran
 * Global Cybersoft VN.
 * Floor 3-4-5, Helios Building, Quang Trung Software City
 */
package com.minh.tran.sg.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import swing2swt.layout.FlowLayout;

import com.minh.tran.sg.Configuration;

public class ConfigView
{
    /** The shell. */
    protected Shell shell;

    /** The Text link download nct. */
    private Text txtFormatNCTSong;

    /** The txt format nct playlist. */
    private Text txtFormatNCTPlaylist;

    /** The config. */
    private final Configuration config;

    private Text txtKeyLinkNCT;

    /**
     * Instantiates a new config view.
     */
    public ConfigView(final Configuration conf)
    {
        this.config = conf;
    }

    /*public static void main(String[] args)
    {
        try
        {
            ConfigView window = new ConfigView();
            window.open();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }*/

    /**
     * Open the window.
     */
    public void open()
    {
        Display display = Display.getDefault();
        this.createContents();
        this.shell.open();
        this.shell.layout();
        this.config.load();
        this.initValues();
        while (!this.shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }

    }

    /**
     * Inits the values.
     */
    private void initValues()
    {
        this.txtFormatNCTSong.setText(this.config.getLinkNCTFormatSong());
        this.txtFormatNCTPlaylist.setText(this.config.getLinkNCTFormatPlaylist());
        this.txtKeyLinkNCT.setText(this.config.getKeyLinkNCT());
    }

    /**
     * Create contents of the window.
     */
    protected void createContents()
    {
        this.shell = new Shell();
        this.shell.addDisposeListener(new DisposeListener()
        {
            @Override
            public void widgetDisposed(DisposeEvent arg0)
            {

            }
        });
        this.shell.setSize(450, 300);
        this.shell.setText("SWT Application");
        this.shell.setLayout(new FillLayout(SWT.HORIZONTAL));

        Composite composite = new Composite(this.shell, SWT.NONE);
        composite.setLayout(new RowLayout(SWT.HORIZONTAL));

        Group grpNCT = new Group(composite, SWT.NONE);
        grpNCT.setLayoutData(new RowData(430, SWT.DEFAULT));
        grpNCT.setText("NCT");
        grpNCT.setLayout(new GridLayout(2, false));

        Label lblFormatNCTSong = new Label(grpNCT, SWT.NONE);
        lblFormatNCTSong.setText("Song");

        this.txtFormatNCTSong = new Text(grpNCT, SWT.BORDER);
        this.txtFormatNCTSong.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        Label lblFormatNCTPlaylist = new Label(grpNCT, SWT.NONE);
        lblFormatNCTPlaylist.setText("Playlist");

        this.txtFormatNCTPlaylist = new Text(grpNCT, SWT.BORDER);
        this.txtFormatNCTPlaylist.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

        Label lblKeyLinkNCT = new Label(grpNCT, SWT.NONE);
        lblKeyLinkNCT.setText("Key");

        this.txtKeyLinkNCT = new Text(grpNCT, SWT.BORDER);
        this.txtKeyLinkNCT.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        Group grpZing = new Group(composite, SWT.NONE);
        grpZing.setText("Zing");
        grpZing.setLayoutData(new RowData(430, 100));

        Composite comActions = new Composite(composite, SWT.NONE);
        comActions.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        comActions.setLayoutData(new RowData(430, 31));

        Button btnSave = new Button(comActions, SWT.NONE);
        btnSave.addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetSelected(SelectionEvent e)
            {
                ConfigView.this.saveConfig();
            }
        });
        btnSave.setText("Save");
    }

    /**
     * Save config.
     */
    protected void saveConfig()
    {
        this.config.setLinkNCTFormatSong(this.txtFormatNCTSong.getText());
        this.config.setLinkNCTFormatPlaylist(this.txtFormatNCTPlaylist.getText());
        this.config.setKeyLinkNCT(this.txtKeyLinkNCT.getText());
        this.config.save();
        this.shell.close();
    }
}
